[Music]
Welcome to Working with Node.js Modules. After watching this video, you will be able
to: Describe Node.js packages. Import Node.js modules into your script. Export functions and properties from a module, and access exported properties from a module. A package consists of one or more modules. The package.json file describes details about
a Node.js module. If a module does not have a package.json file,
Node.js assumes that the main class is named index.js. To specify a different main script for your
module, specify a relative path to the Node.js script from the module directory. This is an example of a package.json file. The name and version fields form a unique
identifier for the module; for example, today-1.0.0. The main field lists a path to the main Node.js
script; in this example, the today.js script in the
lib subdirectory. Package.json defines many other fields. For example, license states the module's usage
rights. You can use the require function to import
a Node.js module. The require statement assumes that scripts
have a file extension of .js. The require function creates an object that
represents the imported Node.js module. In this example, a Node,js script file that
is named today.js is in the same directory as your application. When you call require with the name of a subdirectory,
Node.js looks for a script file with the same name as the subdirectory. If the script file does not exist, the function
assumes that the name is the name of a directory and looks for a script named index.js within
that directory. To import a Node.js module that consists of
a single script, use the require function with a relative path to the script file. In this example, the main application is in
the Node.js script file. Hello.js makes a require function call to
the today.js script file. This example uses the same hello.js Node.js
file. The Node.js module is saved in a directory
named mod_today. The actual script file is saved in index.js. When hello.js makes a call to the require
function in the mod_today directory, the script file checks whether there is a file named
index.js. This is the default name for a script in a
Node.js module. Each Node.js module has an implicit exports
object. To make a function or a value available to
Node.js applications that import your module, add a property to exports. In this example, the dayOfWeek property is
added to the exports object. Then, dayOfWeek is assigned an anonymous function
that returns the day of the week. For example, if the dayOfWeek function returns
1, this value maps to Monday. When you import a Node.js module, the require
function returns a JavaScript object that represents an instance of the module. For example, the today variable is an instance
of the today Node.js module that is called "today." To access the properties of the module, retrieve
the property from the variable. In the same example, today.dayOfWeek represents
the current exported property from the today Node.js module. In this video, you learned that: Every package has a package.json file that
describes details about a Node.js module. To make a function or a value available to
Node.js applications that import your module, add a property to the implicit exports object and when you import a Node.js module, the require
function returns a JavaScript object that represents an instance of the module.